package com.suse.service.community.controller;

import com.suse.common.config.response.R;
import com.suse.service.community.constant.EntityTypeConstant;
import com.suse.service.community.constant.TopicConstant;
import com.suse.service.community.kafka.Event;
import com.suse.service.community.kafka.EventProducer;
import com.suse.service.community.service.FollowService;
import com.suse.service.community.util.UserHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * <p>
 * 关注前端控制器
 * </p>
 *
 * @author 辜玉彬
 * @since 2022-09-02 18:29
 */
@Controller
public class FollowController {

    @Resource
    private FollowService followService;

    @Resource
    private EventProducer eventProducer;

    @GetMapping("/follow/{targetId}")
    @ResponseBody
    public R follow(@PathVariable("targetId") int targetId) {
        followService.follow(targetId);
        // 触发关注事件
        Integer userId = UserHolder.getUser().getId();
        Event event = new Event();
        event.setTopic(TopicConstant.FOLLOW);
        event.put("userId",userId);
        event.put("entityType", EntityTypeConstant.USER);
        event.put("entityId",targetId);
        event.put("entityUserId",targetId);
        eventProducer.fireEvent(event);
        return R.success("关注成功！");
    }

    @GetMapping("/unfollow/{targetId}")
    @ResponseBody
    public R unfollow(@PathVariable("targetId") int targetId) {
        followService.unfollow(targetId);
        return R.success("取消关注成功！");
    }

}
